package com.baidu.shoppingmall.comment.service.impl;

import com.baidu.shoppingmall.comment.dao.CommentInfoDAO;
import com.baidu.shoppingmall.comment.dao.CommentTimerDAO;
import com.baidu.shoppingmall.comment.domain.CommentInfoPO;
import com.baidu.shoppingmall.comment.domain.CommentTimerPO;
import com.baidu.shoppingmall.comment.service.CommentTimerService;
import com.baidu.shoppingmall.common.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class CommentTimerServiceImpl extends ServiceImpl<CommentTimerDAO, CommentTimerPO> implements CommentTimerService {

    @Autowired
    private CommentTimerDAO commentTimerDAO;

    @Autowired
    private CommentInfoDAO commentInfoDAO;

    private static final Logger LOGGER=LoggerFactory.getLogger(CommentTimerServiceImpl.class);

    /**
     * 用户评论后  改变评论状态为手动评论
     * @param orderInfoId
     * @return
     */
    @Override
    public Boolean timingChangeState(Long orderInfoId) {
        try {
            CommentTimerPO commentTimerPO = new CommentTimerPO();
            commentTimerPO.setOrderItemId(orderInfoId);
            commentTimerPO.setCommentStatus(0);
            QueryWrapper<CommentTimerPO> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = new HashMap<>();
            map.put("order_item_id", orderInfoId);
            queryWrapper.allEq(map);
            commentTimerDAO.update(commentTimerPO,queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("error", e);
            return false;
        }
        return true;
    }

    /**
     * 当用户购买商品时，绑定一个定时任务
     * @return
     */
    @Override
    public Boolean timingComment(CommentInfoPO commentInfoPO) {

        try {
            QueryWrapper<CommentTimerPO> queryWrapper = new QueryWrapper<>();
            Map<String, Object> map = new HashMap<>();
            map.put("order_item_id", commentInfoPO.getOrderInfoId());
            queryWrapper.allEq(map);
            Integer commentStatus = commentTimerDAO.selectOne(queryWrapper).getCommentStatus();
            if (commentStatus==1){
                commentInfoPO.setTotalScore(5);
                commentInfoPO.setGoodsScore(5);
                commentInfoPO.setCustomerServiceScore(5);
                commentInfoPO.setLogisticsScore(5);
                commentInfoPO.setCommentContent("默认自动好评");
                commentInfoPO.setDefaultComment(1);
                commentInfoPO.setCommentStatus(2);
                commentInfoPO.setCommentType(1);
                commentInfoPO.setShowPictures(0);
                commentInfoPO.setCreateTime(DateUtils.getCurrentTime());
                commentInfoPO.setUpdateTime(DateUtils.getCurrentTime());
                commentInfoDAO.insert(commentInfoPO);
            }
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("error", e);
            return false;
        }
        return true;
    }

    /**
     * 当户用购买商品时 设置为自动评论状态
     * @param orderInfoId
     * @return
     */
    @Override
    public Boolean commentAutomatic(Long orderInfoId) {
        try {
            CommentTimerPO commentTimerPO = new CommentTimerPO();
            commentTimerPO.setOrderItemId(orderInfoId);
            commentTimerPO.setCommentStatus(1);
            commentTimerDAO.insert(commentTimerPO);
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.error("error", e);
            return false;
        }
        return true;
    }
}
